package com.cake.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cake.base.util.ReturnUtils;
import com.cake.entity.XxCoupon;
import com.cake.service.inter.IXxCouponService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 优惠券控制层
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "coupon")
public class CouponController {

	@Autowired
	private IXxCouponService xxCouponService;
	
	/**
	 * 获取所有的优惠券
	 * @return
	 * @throws Exception 
	 */
	@PostMapping(value = "selectCoupons")
	public Object selectCoupons(@RequestBody XxCoupon xxCoupon) throws Exception{
		/**
		 * 分页查询设置
		 */
		PageHelper.startPage(xxCoupon.getCurrentPage(), xxCoupon.getPageSize());
		List<XxCoupon> list = xxCouponService.selectByAttributeAnd(xxCoupon);
		/**
		 * 分页对象返回
		 */
		PageInfo<XxCoupon> page = new PageInfo<>(list);
		return ReturnUtils.forNoramlPage("000000", list, 
				page.getPageNum(), page.getPageSize(), page.getPages(), (int)page.getTotal());
	}
	
	/**
	 *	根据id获取一条优惠券详情
	 * @param couponId
	 * @return
	 * @throws Exception 
	 */
	@GetMapping(value = "selectCouponById")
	public Object selectCouponById(Long couponId) throws Exception{
		return ReturnUtils.forNoramlSuccess(xxCouponService.selectCouponDetails(couponId));
	}
	
	/**
	 * 添加一条优惠券
	 * @return
	 * @throws Exception 
	 */
	@PostMapping(value = "insertOrUpdateCoupon")
	public Object insertCoupon(@RequestBody XxCoupon xxCoupon) throws Exception{
		xxCouponService.insertOrUpdateCoupon(xxCoupon);
		return ReturnUtils.forNoraml("000000", "操作成功");
	}
	
	/**
	 * 禁用优惠券
	 * @return
	 * @throws Exception 
	 */
	@PostMapping(value = "forbiddenCoupon")
	public Object forbiddenCoupon(Long couponId) throws Exception{
		XxCoupon xxCoupon = xxCouponService.selectByPrimaryKey(couponId);
		if(xxCoupon != null){
			xxCoupon.setIsExchange(true);
			xxCouponService.updateByPrimaryKeySelective(xxCoupon);
		}
		return ReturnUtils.forNoraml("000000", "操作成功");
	}
}
